package com.example.no1admin.controller;

import com.example.no1admin.pojo.dto.order.OrderAddNewDTO;
import com.example.no1admin.pojo.dto.order.OrderEditDTO;
import com.example.no1admin.pojo.vo.order.OrderDetailVO;
import com.example.no1admin.pojo.vo.order.OrderListItemVO;
import com.example.no1admin.service.IOrderService;
import com.example.no1admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单管理模块")
@RequestMapping("/orders")
@Slf4j
@RestController
public class OrderController {
    @Autowired
    IOrderService orderService;

    @ApiOperation("创建订单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody OrderAddNewDTO orderAddNewDTO){
        log.debug("接受到的请求参数:{}",orderAddNewDTO);
        orderService.addNew(orderAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除订单")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("接收到删除订单的请求");
        orderService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改订单")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult updateById(OrderEditDTO orderEditDTO){
        log.debug("接收到修改订单的请求");
        orderService.updateById(orderEditDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询订单详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/detail")
    public JsonResult getById(Long id){
        log.debug("接收到查询订单详情的请求");
       OrderDetailVO orderDetailVO = orderService.getById(id);
        return JsonResult.ok(orderDetailVO);
    }

    @ApiOperation("订单列表")
    @ApiOperationSupport(order = 500)
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询订单列表的请求");
        List<OrderListItemVO> orders = orderService.list();
        return JsonResult.ok(orders);
    }
}
